home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / Replacements / cpdist_0_17.lha / cpdist-0.17 / source / test.c < prev    next >
C/C++ Source or Header  |  1994-05-28  |  3KB  |  132 lines

  1. #include <stdio.h>
  2.  
  3. #include <exec/types.h>
  4. #include <exec/ports.h>
  5. #include <exec/memory.h>
  6.  
  7. #include <libraries/dos.h>
  8. #include <libraries/dosextens.h>
  9.  
  10. main()
  11. {
  12.   BPTR console;
  13.   BPTR input;
  14.  
  15.   console= Open("*",1005);
  16.   input= Input();
  17.  
  18.   if( BADDR(input) )
  19.   {
  20.     char buffer[10];
  21.     long notty= 0;
  22.  
  23.     /* is 'input' a tty? */
  24.     (void)Seek(input, 2L, OFFSET_BEGINNING);
  25.     notty= Seek(input, 0L, OFFSET_CURRENT);
  26.  
  27.     (void)Seek(input, 0L, OFFSET_END);
  28.     notty+= Seek(input, 0L, OFFSET_BEGINNING);
  29.  
  30.     printf("notty= %ld\n",notty);
  31.  
  32.     /*
  33.     */
  34.  
  35.     if( IsInteractive(input) )
  36.     {
  37.       struct MsgPort *mp;
  38.       long arg[1], res;
  39.  
  40.       printf("Input() returned an interactive handle.\n");
  41.  
  42.       mp = ((struct FileHandle *)(BADDR(input)))->fh_Type;
  43.       arg[0]= -1L; /* -1= RAW, 0= COOKED */
  44.  
  45.       res= SendPacket(mp, ACTION_SCREEN_MODE, arg, 1);
  46.       printf("SendPacket() returned %ld\n",res);
  47.      
  48.       { int i; for(i=0; i<sizeof(buffer); buffer[i++]='\0') ; }
  49.  
  50.       res= Read(input, buffer, (long)sizeof(buffer)-1 );
  51.       printf("Read() returned %ld, buffer[]= \"%s\"\n",res,buffer);
  52.  
  53.       arg[0]= 0L; /* 0= COOKED */
  54.  
  55.       res= SendPacket(mp, ACTION_SCREEN_MODE, arg, 1);
  56.       printf("SendPacket() returned %ld\n",res);
  57.     }
  58.     else
  59.     { printf("Input() is not interactive.\n");
  60.       if(console)
  61.       {
  62.     long res;
  63.     { int i; for(i=0; i<sizeof(buffer); buffer[i++]='\0') ; }
  64.     res= Read(console, buffer, (long)sizeof(buffer)-1 );
  65.     printf("Read() returned %ld, buffer[]= \"%s\"\n",res,buffer);
  66.       }
  67.       else printf("Open() failed.  No console window?\n");
  68.     }
  69.   }
  70.   else printf("Input() failed.  No console?\n");
  71.  
  72.   if(console) Close(console);
  73.  
  74.   exit(0);
  75. }
  76.  
  77. /*
  78.  * Function - SendPacket written by Phil Lindsay, Carolyn Scheppner, and Andy
  79.  * Finkel. This function will send a packet of the given type to the Message
  80.  * Port supplied.
  81.  */
  82.  
  83. typedef struct StandardPacket StandardPacket;
  84.  
  85. long
  86. SendPacket(pid, action, args, nargs)
  87. struct MsgPort *pid;    /* process indentifier ... (handler's message port) */
  88. long action;    /* packet type ... (what you want handler to do) */
  89. long *args;     /* a pointer to an argument list */
  90. long nargs;     /* number of arguments in list */
  91. {
  92.     struct MsgPort *replyport;
  93.     StandardPacket *packet;
  94.     long count, *pargs, res1;
  95.  
  96.     replyport= (struct MsgPort *)CreatePort(NULL, 0L);
  97.     if (!replyport)
  98.     return(0);
  99.  
  100.     /* Allocate space for a packet, make it public and clear it */
  101.  
  102.     packet = (StandardPacket *)
  103.       AllocMem(sizeof(StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
  104.  
  105.     if (!packet) {
  106.     DeletePort(replyport);
  107.     return(0);
  108.     }
  109.     packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
  110.     packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
  111.     packet->sp_Pkt.dp_Port = replyport;
  112.     packet->sp_Pkt.dp_Type = action;
  113.  
  114.     /* copy the args into the packet */
  115.  
  116.     pargs = &(packet->sp_Pkt.dp_Arg1);      /* address of first argument */
  117.     for (count = 0; count < nargs; count++)
  118.     pargs[count] = args[count];
  119.  
  120.     PutMsg(pid, &packet->sp_Msg);   /* send packet */
  121.  
  122.     WaitPort(replyport);
  123.     GetMsg(replyport);
  124.  
  125.     res1 = packet->sp_Pkt.dp_Res1;
  126.  
  127.     FreeMem(packet, sizeof(StandardPacket));
  128.     DeletePort(replyport);
  129.  
  130.     return (res1);
  131. }
  132.